import request from "@/utils/request";
import { getToken, removeToken } from "@/utils/auth";
import { login } from '@/api/login.js'
import router from '@/router/index'

// 查询列表
export function listApi(query) {
  return request({
    url: "/p/user/page",
    method: "get",
    params: query,
  });
}

// 查询列表不分页
export function listAllApi() {
  return request({
    url: "/p/user/list",
    method: "get",
  });
}

// 新增
export function addApi(data) {
  return request({
    url: "/p/user",
    method: "post",
    data: data,
  });
}

// 修改
export function updateApi(data) {
  return request({
    url: "/p/user",
    method: "put",
    data: data,
  });
}

// 详情
export function detailApi(id) {
  return request({
    url: `/p/user/${id}`,
    method: "get",
  });
}

// 获取用户信息
export function getInfo() {
  return new Promise(async (resolve, reject) => {
    const token = getToken()
    if (!token) {
      reject(false)
      return
    }
    try {
      await login(token, '', true)
      resolve({
        msg: "操作成功",
        code: 200,
        permissions: ["*:*:*"],
        roles: ["admin"],
        user: {
          createBy: "admin",
          createTime: "2023-04-23 16:11:38",
          updateBy: null,
          updateTime: null,
          remark: "管理员",
          userId: 1,
          deptId: 103,
          userName: "admin",
          nickName: "若依",
          email: "ry@163.com",
          phonenumber: "15888888888",
          sex: "1",
          avatar: "",
          password: "$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2",
          status: "0",
          delFlag: "0",
          loginIp: "101.247.128.187",
          loginDate: "2023-11-18T22:53:38.000+08:00",
          dept: {
            createBy: null,
            createTime: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            deptId: 103,
            parentId: 101,
            ancestors: "0,100,101",
            deptName: "研发部门",
            orderNum: 1,
            leader: "若依",
            phone: null,
            email: null,
            status: "0",
            delFlag: null,
            parentName: null,
            children: [],
          },
          roles: [
            {
              createBy: null,
              createTime: null,
              updateBy: null,
              updateTime: null,
              remark: null,
              roleId: 1,
              roleName: "超级管理员",
              roleKey: "common",
              roleSort: 1,
              dataScope: "1",
              menuCheckStrictly: false,
              deptCheckStrictly: false,
              status: "0",
              delFlag: null,
              flag: false,
              menuIds: null,
              deptIds: null,
              permissions: null,
              admin: true,
            },
          ],
          roleIds: null,
          postIds: null,
          roleId: null,
          admin: true,
        },
      });
    } catch (error) {
      removeToken()
      router.push('/login')
    }

  });
}
